package studentsystem1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;

public class DeleteForm extends JFrame implements ActionListener {
    JLabel labName = new JLabel("姓名：");
    JLabel labDate = new JLabel("学号：");
    JLabel labScore = new JLabel("成绩：");
    JTextField txtName = new JTextField(20);
    JTextField txtDate = new JTextField(18);
    JTextField txtScore = new JTextField(20);
    JButton btnDelete = new JButton("删除");
    JButton btnCancel = new JButton("取消");
    JButton btnQuery = new JButton("查询");
    JPanel pan = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();
    JPanel pan3 = new JPanel();
    JPanel pan4 = new JPanel();
    Connection con;
    Statement sql;
    ResultSet rs;

    DeleteForm() {
        super("删除数据");
        setSize(400, 300);
        pan1.add(labName);
        pan1.add(txtName);
        pan2.add(labDate);
        pan2.add(txtDate);
        pan3.add(labScore);
        pan3.add(txtScore);
        pan4.add(btnQuery);
        pan4.add(btnDelete);
        pan4.add(btnCancel);
        pan.setLayout(new GridLayout(3, 1));
        pan.add(pan1);
        pan.add(pan2);
        pan.add(pan3);
        getContentPane().add(pan, "Center");
        getContentPane().add(pan4, "South");
        btnQuery.addActionListener(this);
        btnDelete.addActionListener(this);
        btnCancel.addActionListener(this);
        btnDelete.setEnabled(false);//取消删除按钮的功能
        txtDate.setEditable(false);
        txtScore.setEditable(false);
        setVisible(true);
        txtName.requestFocus();
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == btnCancel) {
            dispose();
        } else if (ae.getSource() == btnQuery) {      //实现查询功能
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
                String user = "root";
                String passwd = "123456";
                con = DriverManager.getConnection(url, user, passwd);
                sql = con.createStatement();
                rs = sql.executeQuery("select * from student where username='" + txtName.getText() + "'");
                if (rs.next()) {
                    txtName.setText(rs.getString("username"));
                    txtScore.setText(new Integer(rs.getInt("stu_score")).toString());
                    txtDate.setText(new Integer(rs.getInt("stu_time")).toString());
                    btnDelete.setEnabled(true);//使删除按钮功能实现
                    txtDate.setEditable(true);
                    txtScore.setEditable(true);
                } else {
                    System.out.println("不存在该记录！");
                    btnDelete.setEnabled(false);
                    txtName.setText("");
                    txtScore.setText("");
                    txtDate.setText("");
                    txtDate.setEditable(false);
                    txtScore.setEditable(false);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else if (ae.getSource() == btnDelete)        //实现删除功能
        {
            try {
                sql.executeUpdate("delete from student where username='" + txtName.getText() + "'");
                System.out.println("记录删除完毕！");
                btnDelete.setEnabled(false);
                txtName.setText("");
                txtScore.setText("");
                txtDate.setText("");
                txtDate.setEditable(false);
                txtScore.setEditable(false);
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new DeleteForm();
    }
}